package com.linkstec.json;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;

import com.alibaba.fastjson.JSON;

public class AppendInfoTest {

	private static Logger logger = Logger.getLogger(AppendInfoTest.class);
    /**
     * 模板参数键、值获取
     *
     * @param originalStr 如：[客户编号=79001465;客户姓名=刘**;销户日期=20161109]
     * @return LMap<String, String> map
     */
    protected static Map<String, String> getAppendInfoValueAndKey(String originalStr) {
        // 以分号分割 [客户编号=79001465;客户姓名=刘**;销户日期=20161109]
        String[] strs = originalStr.split(";");
        Map<String, String> map = new HashMap<String, String>();
        for (String str : strs) {
            // [客户编号,79001465]
            String destStr = null;
            if (str != null) {
                Pattern p = Pattern.compile("\t|\r|\n");
                Matcher m = p.matcher(str);
                destStr = new String(m.replaceAll(""));
            }
            // 判断字符串中是否含有“=”
            if (destStr.contains("=")) {
                String[] strings = destStr.split("=", 2);
                if (strings.length == 2) {
                    map.put(strings[0], strings[1]);
                }
            }
        }
        return map;
    }
    @SuppressWarnings("unchecked")
	public static void main(String[] args) {
    	Map<String, String> appenMap = null;
    	Map<String, String> appenMapJson = null;
    	
    	// json格式
    	String appenJson = "{\"客户编号\":\"79001465\",\"客户姓名\":\"刘**\",\"销户日期\":\"20161109\"}";
    	long startJson = System.currentTimeMillis();
    	for(int i = 0; i < 1000000; i++) {
    		appenMapJson = JSON.parseObject(appenJson, HashMap.class);
    	}
    	long endJson = System.currentTimeMillis();
    	logger.info("json耗时:" + (endJson-startJson));
    	
    	//分号分割
    	String appen = "客户编号=79001465;客户姓名=刘**;销户日期=20161109";
    	long start = System.currentTimeMillis();
    	for(int i = 0; i < 300000; i++) {
    		appenMap = getAppendInfoValueAndKey(appen);
    	}
    	long end = System.currentTimeMillis();
    	logger.info("分号耗时:" + (end-start));

    	logger.info(appenMap);
    	logger.info(appenMapJson);
	}
}